package cn.patient.pp.module.patients.controller.admin.vo;

import com.fasterxml.jackson.annotation.JsonSetter;
import lombok.*;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.*;
import io.swagger.v3.oas.annotations.media.Schema;
import cn.patient.pp.framework.common.pojo.PageParam;
import org.springframework.format.annotation.DateTimeFormat;

import static cn.patient.pp.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;

@Schema(description = "管理后台 - 患者总分页 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class PatientsDOPageReqVO extends PageParam {


    @Schema(description = "管理机构id", example = "8972")
    private List<Long> deptIds;

    @Schema(description = "患者姓名", example = "王五")
    private String patientName;

    @Schema(description = "患者编号")
    private String patientCode;

    @Schema(description = "证件号码")
    private String idNumber;

    @Schema(description = "下次随访日期")
    private String[] nextFollowupDate;

    @Schema(description = "患者性别")
    private String gender;

    @Schema(description = "现住详细地址")
    private String currentAddress;

    @Schema(description = "末次随访时间")
    private String[] lastFollowupDate;

    @Schema(description = "住院状态", example = "1")
    private String hospitalizationStatus;

    @Schema(description = "危险行为")
    private String dangerousBehavior;

    @Schema(description = "危险性评估")
    private String riskAssessment;

    @JsonSetter("lastFollowupDate")
    public void setLastFollowupDate(String[] dates) {
        if (dates == null) {
            this.lastFollowupDate = null;
            return;
        }
        DateTimeFormatter fmt1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter out  = DateTimeFormatter.ofPattern("yyyyMMdd");

        this.lastFollowupDate = Arrays.stream(dates)
                .map(s -> {
                    // Java 8 下判断空白：s == null || s.trim().isEmpty()
                    if (s == null || s.trim().isEmpty()) {
                        return s;
                    }
                    try {
                        LocalDateTime dt = LocalDateTime.parse(s, fmt1);
                        return dt.format(out);
                    } catch (DateTimeParseException ex) {
                        // ISO-8601 格式解析
                        Instant inst = Instant.parse(s);
                        LocalDateTime dt2 = LocalDateTime.ofInstant(inst, ZoneId.systemDefault());
                        return dt2.format(out);
                    }
                })
                .toArray(String[]::new);
    }

    @JsonSetter("nextFollowupDate")
    public void setNextFollowupDate(String[] dates) {
        if (dates == null) {
            this.nextFollowupDate = null;
            return;
        }
        DateTimeFormatter fmt1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter out  = DateTimeFormatter.ofPattern("yyyyMMdd");

        this.nextFollowupDate = Arrays.stream(dates)
                .map(s -> {
                    // Java 8 下判断空白：s == null || s.trim().isEmpty()
                    if (s == null || s.trim().isEmpty()) {
                        return s;
                    }
                    try {
                        LocalDateTime dt = LocalDateTime.parse(s, fmt1);
                        return dt.format(out);
                    } catch (DateTimeParseException ex) {
                        // ISO-8601 格式解析
                        Instant inst = Instant.parse(s);
                        LocalDateTime dt2 = LocalDateTime.ofInstant(inst, ZoneId.systemDefault());
                        return dt2.format(out);
                    }
                })
                .toArray(String[]::new);
    }


}